//
//  LBProgressHUD.m
//  LBBase
//
//  Created by gary on 15/7/1.
//  Copyright (c) 2015年 Lebo. All rights reserved.
//

#import "LBProgressHUD.h"
#import "CommonDefine.h"

#ifdef BUILD_USE_SWIFT
#import "LBBase-Swift.h"
#else
#import "AppDelegate.h"
#endif

#define kAlertViewBgColor       [UIColor colorWithRed:17.f / 255.f green:46.f / 255.f blue:86.f / 255.f alpha:0.9f]
#define HUDMaxInvaliedTime      60
#define HUDMessageInvaliedTime  1.8

@implementation LBProgressHUD

+ (LBProgressHUD *)sharedInstance {
  AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  for (UIView *tmpview in [delegate.window subviews]) {
    if ([tmpview isKindOfClass:[LBProgressHUD class]]) {
      [tmpview removeFromSuperview];
    }
  }
  LBProgressHUD *hudAlert = [[LBProgressHUD alloc] initWithView:delegate.window];
  [delegate.window addSubview:hudAlert];
  [hudAlert show:YES];

  return hudAlert;
}

+ (LBProgressHUD *)sharedInstanceWithView:(UIView *)view {
  LBProgressHUD *hudAlert = [[LBProgressHUD alloc] initWithView:view];
  [view addSubview:hudAlert];
  [hudAlert show:YES];

  return hudAlert;
}

+ (void)showMessage:(NSString *)message {
  dispatch_async(dispatch_get_main_queue(), ^{
    LBProgressHUD *hudAlert = [LBProgressHUD sharedInstance];

    hudAlert.mode = MBProgressHUDModeText;
    hudAlert.detailsLabelText = message;
    hudAlert.detailsLabelFont = hudAlert.labelFont;
    hudAlert.yOffset = 150.0f;
    hudAlert.removeFromSuperViewOnHide = YES;
    hudAlert.alpha = 0.75;
    [hudAlert hide:YES afterDelay:HUDMessageInvaliedTime];
  });
}

+ (void)showMessage:(NSString *)message withView:(UIView *)view {
  dispatch_async(dispatch_get_main_queue(), ^{
    LBProgressHUD *hudAlert = [LBProgressHUD sharedInstanceWithView:view];

    hudAlert.mode = MBProgressHUDModeText;
    hudAlert.detailsLabelText = message;
    hudAlert.detailsLabelFont = hudAlert.labelFont;
    hudAlert.yOffset = 0.f;
    hudAlert.removeFromSuperViewOnHide = YES;
    [hudAlert setColor:kAlertViewBgColor];
    [hudAlert hide:YES afterDelay:HUDMessageInvaliedTime];
  });
}

+ (void)showMessage:(NSString *)message afterDelay:(NSInteger)time {
  dispatch_async(dispatch_get_main_queue(), ^{
    LBProgressHUD *hudAlert = [LBProgressHUD sharedInstance];

    hudAlert.mode = MBProgressHUDModeText;
    hudAlert.detailsLabelText = message;
    hudAlert.detailsLabelFont = hudAlert.labelFont;
    hudAlert.yOffset = 150.0f;
    hudAlert.removeFromSuperViewOnHide = YES;
    hudAlert.alpha = 0.75;
    [hudAlert hide:YES afterDelay:time];
  });
}

+ (void)showMessage:(NSString *)message withView:(UIView *)view afterDelay:(NSInteger)time {
  dispatch_async(dispatch_get_main_queue(), ^{
    LBProgressHUD *hudAlert = [LBProgressHUD sharedInstanceWithView:view];

    hudAlert.mode = MBProgressHUDModeText;
    hudAlert.labelText = message;
    hudAlert.yOffset = 0.f;
    hudAlert.removeFromSuperViewOnHide = YES;
    [hudAlert setColor:kAlertViewBgColor];
    [hudAlert hide:YES afterDelay:time];
  });
}

+ (void)showMessage:(NSString *)message andImage:(NSString*)imageName afterDelay:(NSInteger)time {
  dispatch_async(dispatch_get_main_queue(), ^{
    LBProgressHUD *hudAlert = [LBProgressHUD sharedInstance];

    hudAlert.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    hudAlert.mode = MBProgressHUDModeCustomView;
    hudAlert.labelText = message;
    hudAlert.yOffset = 0.f;
    hudAlert.removeFromSuperViewOnHide = YES;
    [hudAlert setColor:kAlertViewBgColor];
    [hudAlert hide:YES afterDelay:time];
  });
}

+ (void)showMessage:(NSString *)message
           withView:(UIView *)view
           andImage:(NSString*)imageName
         afterDelay:(NSInteger)time {
  dispatch_async(dispatch_get_main_queue(), ^{
    LBProgressHUD *hudAlert = [LBProgressHUD sharedInstanceWithView:view];

    hudAlert.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    hudAlert.mode = MBProgressHUDModeCustomView;
    hudAlert.labelText = message;
    hudAlert.yOffset = 0.f;
    hudAlert.removeFromSuperViewOnHide = YES;
    [hudAlert setColor:kAlertViewBgColor];
    [hudAlert hide:YES afterDelay:time];
  });
}

+ (void)showWaitAlertWithMessage:(NSString *)text {
  dispatch_async(dispatch_get_main_queue(), ^{
    LBProgressHUD *hudAlert = [LBProgressHUD sharedInstance];
    hudAlert.labelText = text;
    hudAlert.removeFromSuperViewOnHide = YES;
  });
}

+ (void)showWaitAlertWithMessage:(NSString *)text withView:(UIView *)view {
  dispatch_async(dispatch_get_main_queue(), ^{
    LBProgressHUD *hudAlert = [LBProgressHUD sharedInstanceWithView:view];
    hudAlert.labelText = text;
    hudAlert.removeFromSuperViewOnHide = YES;
  });
}

+ (void)showWaitAlert {
  dispatch_async(dispatch_get_main_queue(), ^{
    [LBProgressHUD sharedInstance];
  });
}

+ (void)showWaitAlertWithView:(UIView *)view {
  dispatch_async(dispatch_get_main_queue(), ^{
    [LBProgressHUD sharedInstanceWithView:view];
  });
}

+ (void)hideProgressHUD {
  dispatch_async(dispatch_get_main_queue(), ^{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    for (UIView *tmpview in [delegate.window subviews]) {
      if ([tmpview isKindOfClass:[LBProgressHUD class]]) {
        [tmpview removeFromSuperview];
        [(LBProgressHUD *)tmpview hide:YES];
      }
    }
  });
}

+ (void)hideProgressHUDWithView:(UIView *)view {
  dispatch_async(dispatch_get_main_queue(), ^{
    for (UIView *tmpview in [view subviews]) {
      if ([tmpview isKindOfClass:[LBProgressHUD class]]) {
        [tmpview removeFromSuperview];
        [(LBProgressHUD *)tmpview hide:YES];
      }
    }
  });
}

+ (void)showSelectAlertView:(NSString *)title actionBlock:(CustomIOS7AlertViewBlock)block {
  CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] init];

  CGFloat viewWidth = 290.0f;
  CGFloat viewHeight= 100.0f;
  CGFloat hDistance = 20.0f;
  CGFloat vDistance = 10.0f;

  UIView *containView = [[UIView alloc] initWithFrame:MakeRect(0, 0, viewWidth, viewHeight)];

  CGFloat lableWidth = CGRectGetWidth(containView.frame) - 2 * hDistance;
  CGFloat lableHeight = CGRectGetHeight(containView.frame) - 2 * vDistance;
  UILabel *textLable =
        [[UILabel alloc] initWithFrame:MakeRect(hDistance, vDistance, lableWidth, lableHeight)];
  [textLable setText:title];
  [textLable setBackgroundColor:[UIColor clearColor]];
  [textLable setFont:[UIFont fontWithName:@"MyriadPro-Semibold" size:12.0f]];
  [textLable setTextAlignment:NSTextAlignmentCenter];
  [textLable setTextColor:[UIColor blackColor]];
  [textLable setNumberOfLines:0];
  [containView addSubview:textLable];

  [alertView setContainerView:containView];
  [alertView setButtonTitles:[NSMutableArray arrayWithObjects:NSLocalizedString(@"Global_Dialog_OK", @"确定"),
                                                              NSLocalizedString(@"Global_Cancel", @"取消"),
                                                              nil]];
  [alertView setOnButtonTouchUpInside:block];

  [alertView setUseMotionEffects:true];
  [alertView show];
}


@end
